package com.epsi.bowling.data.utils;


import com.mysql.jdbc.log.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.common.util.impl.Log;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author juju
 */
public class HibernateFactory {
    private static SessionFactory sessionFactory;
    
    public static void buildSessionFactory() {   
        try {
          // creating the SessionFactory from hibernate.cfg.xml            
          sessionFactory = new Configuration().configure()
                        .buildSessionFactory();
        } catch (HibernateException ex) {
          throw new ExceptionInInitializerError(ex);
        }
    }
    
   public static Session getSessionFactory() {
       Configuration config = new Configuration()
            .configure("hibernate.cfg.xml");
        ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder();
        srBuilder.applySettings(config.getProperties());
        ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry();
        SessionFactory factory = config.buildSessionFactory(serviceRegistry);
        Session session = factory.openSession();
        return session;
   }
    
}
